package cn.okcmd.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * create by caoqg on 2021/7/17
 * 功能说明:
 */
@Slf4j
public class TestAtomicStamped {

    public static void main(String[] args) {
        AtomicStampedReference<Integer> asr = new AtomicStampedReference<>(new Integer(1000), 1);

        while (true){
            Integer oldVal = asr.getReference();
            int oldStamp = asr.getStamp();
            Integer newVal = oldVal + 10;
            boolean casFlag = asr.compareAndSet(oldVal, newVal, oldStamp, oldStamp + 1);
            if(casFlag){
                log.info("修改数据成功,oldVal:{},newVal:{},oldStamp:{},newStamp:{}", oldVal, newVal, oldStamp, oldStamp + 1);
                break;
            }
        }
    }
}
